<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <!-- 验证表单的必填项
      :model 是表单收集的数据对象
      :rules 验证规则对象
      ref  兜底校验 -->
      <el-form style="padding:0 25px"  :model='formData' :rules='formRules' ref="formRef">
        <!-- form-item prop 指向具体验证的规则 -->
        <el-form-item prop="username">
                <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-search"
            v-model="formData.username">
            </el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="formData.password">
            </el-input>
        </el-form-item>
        <el-form-item prop="repassword">
            <el-input
            placeholder="请确认密码"
            prefix-icon="el-icon-lock"
            v-model="formData.repassword">
            </el-input>
        </el-form-item>
        <el-form-item>
            <el-button style="width:100%" type="primary" @click="regNewuser">注册</el-button>
             <el-link type="info" :underline="false"  @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Reg',
  data(){
    //自定义校验规则函数
    //rule定义的规则
    //value当前表单项的值
    var rePwd = (rule, value, callback) => {
        //callback 回调函数
        // -如果校验事变使用callback 传入一个错误对象
        // -如果校验成功,callback()不传内容,放行
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.formData.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return{
    formData:{
    username:'',
    password:'',
    repassword:'',
    },
    formRules:{
    username:[
        {required:true,message:'请输入用户名',trigger:'blur'},
        {pattern:/^[0-9a-zA-Z]{1,10}$/,message:'请输入1~10的字母或数字',trigger:'blur'}
    ],
    password:[
        {required:true,message:'请输入密码',trigger:'blur'},
        {pattern:/^\S{6,15}$/,message:'请输入6~15的字母或数字',trigger:'blur'}
    ],
    repassword:[
        {required:true,message:'请输入密码',trigger:'blur'},
        {pattern:/^\S{6,15}$/,message:'请输入6~15的字母或数字',trigger:'blur'},
        //validator 调用自定义的函数
        {validator:rePwd,trigger:'blur'}

    ], 
    }
 }
  },
  methods:{
    regNewuser(){
        //1.绑定事件函数
        //2.兜底校验
        this.$refs.formRef.validate(async (valid)=>{
            //兜底验证:验证整个表单是否符合所有规则
            //valid:返回值true||false ,只有所有的表单项校验通过
            if(!valid) return console.log('校验失败');
            //3.发送请求 实现注册
            const {data:res}=await this.$http.post('api/reg',this.formData)
            //console.log(res);
            //3.1成功提示
            if (res.code!==0) return this.$message.error(res.message);
            this.$message.success(res.message);
            this.$router.push('/login')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url('../../assets/images/login_title.png') center no-repeat;
    }
  }
}
</style>

